/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.biz.domain.Vip;
import me.zhengjie.modules.biz.domain.req.VipDetailReq;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.VipService;
import me.zhengjie.modules.biz.domain.vo.VipQueryCriteria;
import me.zhengjie.modules.biz.mapper.VipMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.biz.domain.enums.ErrorCode.*;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-03-22
**/
@Service
@RequiredArgsConstructor
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip> implements VipService {

    private final VipMapper vipMapper;

    @Override
    public PageResult<Vip> queryAll(VipQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(vipMapper.findAll(criteria, page));
    }

    @Override
    public List<Vip> queryAll(VipQueryCriteria criteria){
        return vipMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Vip resources) throws BizException {
        Vip vip = vipMapper.selectOne(new QueryWrapper<Vip>().eq("vip_code", resources.getVipCode()));
        if(vip != null){
            throw new BizException(CODE_ALREADY_EXIST.getCode(),CODE_ALREADY_EXIST.getMessage());
        }
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Vip resources) {
        Vip vip = getById(resources.getId());
        vip.copy(resources);
        saveOrUpdate(vip);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Vip> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Vip vip : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("统招 、专升本 、单招", vip.getName());
            map.put("服务费用", vip.getPrice());
            map.put("渠道：1.统招 2：专升本 3：单招（高职、中职）", vip.getCategory());
            map.put("开始时间", vip.getStartTime());
            map.put("结束时间", vip.getEndTime());
            map.put("文案", vip.getRemark());
            map.put("vip编码", vip.getVipCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Vip getVip(VipDetailReq req) throws BizException {
        if(req.getCategory() == null){
            throw new BizException(VIP_CATEGORY_IS_NULL.getCode(),VIP_CATEGORY_IS_NULL.getMessage());
        }
        return vipMapper.selectOne(new QueryWrapper<Vip>().eq("category",req.getCategory()));
    }
}